#include "collisionPlaneActor.h"
#include "notransformactor.h"
#include "datatypedefine.h"
#include "scene.h"

const float collision_plane_height = 4.0f;

namespace VrDataCore
{
	CollisionPlaneDrawable::CollisionPlaneDrawable(CoreBase::ActorBase* owneractor,const QString& name)
		:Transformable(owneractor,name)
	{
		_root = new osg::Group;
		_planeShapeOne = new CoreBase::PlaneShape;
		//_planeShapeTwo = new CoreBase::PlaneShape;
	}

	void CollisionPlaneDrawable::setControlVertex(const osg::Vec3& start,const osg::Vec3& end)
	{
		_root->removeChildren(0,_root->getNumChildren());
		osg::Vec3 p3(end.x(),end.y(),end.z() + collision_plane_height);
		osg::Vec3 p4(start.x(),start.y(),start.z() + collision_plane_height);

		_planeShapeOne->reset(start,end,p3,p4,osg::Vec2(0.0f,0.0f),osg::Vec2(1.0f,0.0f),
			osg::Vec2(1.0f,1.0f),osg::Vec2(0.0f,1.0f));
		_planeShapeOne->setColor(osg::Vec4(0,1,1,1));
		//_planeShapeTwo->setColor(osg::Vec4(0.0f,1.0f,1.0f,0.5f));
		//osg::Vec3 p5(start);
		//osg::Vec3 p6(end);
		//osg::Vec3 p7(p3);
		//osg::Vec3 p8(p4);
		//_planeShapeTwo->reset(p5,p8,p7,p6,osg::Vec2(0.0f,0.0f),osg::Vec2(1.0f,0.0f),
		//	osg::Vec2(1.0f,1.0f),osg::Vec2(0.0f,1.0f),_ss);
		_root->addChild(_planeShapeOne->getOSGNode());
		//_root->addChild(_planeShapeTwo->getOSGNode());
		getOSGNode()->asGroup()->addChild(_root);
	}

	CollisionPlaneDrawable::~CollisionPlaneDrawable()
	{}

	void CollisionPlaneDrawable::addedToScene(CoreBase::Scene* scene)
	{
		//if(scene == NULL)
		//	getSceneParent()->unRegisterCollidable(this);
		Transformable::addedToScene(scene);
		//if(_haveInScene)
		//{
		//	setActive(false);
			//getSceneParent()->registerCollidable(this);
			//setCollisionMesh(_root);
		//}
	}

	CollisionPlaneActor::CollisionPlaneActor()
	{
		setClassName(CLASS_COLLISION_PLANE);
	}

	CollisionPlaneActor::~CollisionPlaneActor()
	{}

	void CollisionPlaneActor::createDrawable()
	{
		_drawable = new CollisionPlaneDrawable(this);
	}

	void CollisionPlaneActor::setControlVertex(const osg::Vec3& start,const osg::Vec3& end)
	{
		if(start!=end)
		{
			_pointStart = start;
			_pointEnd = end;
			dynamic_cast<CollisionPlaneDrawable*>(_drawable.get())->setControlVertex(_pointStart,_pointEnd);
		}
	}

	void CollisionPlaneActor::getControlVertex(osg::Vec3& start,osg::Vec3& end)
	{
		start = _pointStart;
		end = _pointEnd;
	}
}